package com.example.demo.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.fastjson.JSON;
import com.example.demo.entity.Blank;
import com.example.demo.entity.Choice;
import com.example.demo.entity.Judgment;
import com.example.demo.entity.Short1;
import com.example.demo.service.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author Leri
 */
@Service("questionService")
public class QuestionServiceImpl implements QuestionService {

    private static final Logger LOGGER = LoggerFactory.getLogger(QuestionServiceImpl.class);

    private static String courserID = null;

    @Autowired
    private ChoiceService choiceService;

    @Autowired
    private JudgmentService judgmentService;

    @Autowired
    private BlankService blankService;

    @Autowired
    private Short1Service short1Service;

    @Override
    public Map ChoiceE(InputStream Question, String courseId) {
        courserID = courseId;
        LOGGER.info("aaaaaa");
        Map<String, List> questionMap = new HashMap<>();
        try {
            ExcelReader excelReader = EasyExcel.read(Question).build();

            ChoiceListener choiceListener = new ChoiceListener();
            ReadSheet choiceSheet =
                    EasyExcel.readSheet(0).head(Choice.class).registerReadListener(choiceListener).build();

            JudgmentListener judgmentListener = new JudgmentListener();
            ReadSheet judgmentSheet =
                    EasyExcel.readSheet(1).head(Judgment.class).registerReadListener(judgmentListener).build();

            BlankListener blankListener = new BlankListener();
            ReadSheet blankSheet =
                    EasyExcel.readSheet(2).head(Blank.class).registerReadListener(blankListener).build();

            Short1Listener short1Listener = new Short1Listener();
            ReadSheet short1Sheet =
                    EasyExcel.readSheet(3).head(Short1.class).registerReadListener(short1Listener).build();


            excelReader.read(choiceSheet, judgmentSheet,blankSheet,short1Sheet);

            LOGGER.info(String.valueOf(choiceListener.index));


        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            try {
                Question.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    public class ChoiceListener extends AnalysisEventListener<Choice> {
        private static final int BATCH_COUNT = 5;
        private int index = 0;
        List<Choice> list = new ArrayList<Choice>();
        private Choice choice;

        public ChoiceListener() {
            choice = new Choice();
        }

        public ChoiceListener(Choice choice) {
            this.choice = choice;
        }

        @Override
        public void invoke(Choice data, AnalysisContext context) {
            data.setObj2(courserID);
            data.setUserId(3);
            LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data));
            list.add(data);
            if (list.size() >= BATCH_COUNT) {
                saveData();
                list.clear();
            }
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext context) {
            saveData();
            LOGGER.info("所有数据解析完成！");
        }

        private void saveData() {
            LOGGER.info("{}条数据，开始存储数据库！", list.size());
            index = index + list.size();
            choiceService.insertList(list);
            LOGGER.info("存储数据库成功！");
        }
    }

    public class JudgmentListener extends AnalysisEventListener<Judgment> {
        private static final int BATCH_COUNT = 5;
        private int index = 0;
        List<Judgment> list = new ArrayList<Judgment>();
        private Judgment judgment;

        public JudgmentListener() {
            judgment = new Judgment();
        }

        public JudgmentListener(Judgment judgment) {
            this.judgment = judgment;
        }

        @Override
        public void invoke(Judgment data, AnalysisContext context) {
            data.setObj2(courserID);
            data.setUserId(3);
            LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data));
            list.add(data);
            if (list.size() >= BATCH_COUNT) {
                saveData();
                list.clear();
            }
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext context) {
            saveData();
            LOGGER.info("所有数据解析完成！");
        }

        private void saveData() {
            LOGGER.info("{}条数据，开始存储数据库！", list.size());
            index = index + list.size();
            judgmentService.insertList(list);
            LOGGER.info("存储数据库成功！");
        }
    }

    public class BlankListener extends AnalysisEventListener<Blank> {
        private static final int BATCH_COUNT = 5;
        private int index = 0;
        List<Blank> list = new ArrayList<Blank>();
        private Blank blank;

        public BlankListener() {
            blank = new Blank();
        }

        public BlankListener(Blank blank) {
            this.blank = blank;
        }

        @Override
        public void invoke(Blank data, AnalysisContext context) {
            data.setObj2(courserID);
            data.setUserId(3);
            LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data));
            list.add(data);
            if (list.size() >= BATCH_COUNT) {
                saveData();
                list.clear();
            }
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext context) {
            saveData();
            LOGGER.info("所有数据解析完成！");
        }

        private void saveData() {
            LOGGER.info("{}条数据，开始存储数据库！", list.size());
            index = index + list.size();
            blankService.insertList(list);
            LOGGER.info("存储数据库成功！");
        }
    }

    public class Short1Listener extends AnalysisEventListener<Short1> {
        private static final int BATCH_COUNT = 5;
        private int index = 0;
        List<Short1> list = new ArrayList<Short1>();
        private Short1 short1;

        public Short1Listener() {
            short1 = new Short1();
        }

        public Short1Listener(Short1 short1) {
            this.short1 = short1;
        }

        @Override
        public void invoke(Short1 data, AnalysisContext context) {
            data.setObj2(courserID);
            data.setUserId(3);
            LOGGER.info("解析到一条数据:{}", JSON.toJSONString(data));
            list.add(data);
            if (list.size() >= BATCH_COUNT) {
                saveData();
                list.clear();
            }
        }

        @Override
        public void doAfterAllAnalysed(AnalysisContext context) {
            saveData();
            LOGGER.info("所有数据解析完成！");
        }

        private void saveData() {
            LOGGER.info("{}条数据，开始存储数据库！", list.size());
            index = index + list.size();
            short1Service.insertList(list);
            LOGGER.info("存储数据库成功！");
        }
    }
}
